﻿/*  
 * Helix Engine
 * http://helixengine.codeplex.com/
 * 
 * Copyright (c) 2010 - 2011 Joe Hegarty
 *  
 * This source code is subject to the terms of the Microsoft Public License (Ms-PL). 
 *  
 * Redistribution and use in source and binary forms, with or without modification, 
 * is permitted provided that redistributions of the source code retain the above 
 * copyright notices and this file header. 
 * 
 * For details please see http://www.opensource.org/licenses/ms-pl.html 
 *  
 * All other rights reserved. 
 *
 */  


using System;
using System.Windows.Media.Imaging;

using HelixEngine;
using HelixEngine.Maths;
using HelixEngine.Resources;

namespace HelixGame.World.Entities.Components
{
    /// <summary>
    /// 
    /// </summary>
    public class StaticObjectComponent : EntityComponent
    {
        ResourcePackage _objectPackage;
        State _state = State.Idle;
        string _objectType;
        IsometricPoint _position;
        int _rotation = 0;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectType"></param>
        /// <param name="position"></param>
        /// <param name="rotation"></param>
        public StaticObjectComponent(string objectType, IsometricPoint position, int rotation)
        {
            _objectType = objectType;
            _position = position;
            _rotation = rotation;
        }


        enum State
        {
            WaitingForPackage,
            DrawObject,
            Idle
        }


        /// <summary>
        /// 
        /// </summary>
        public override void OnInitialised()
        {
            Parent.AddEC<GraphicAppearenceComponent>();
            GraphicAppearenceComponent graphicAppearance = null;

            if (Parent.GetEC(ref graphicAppearance))
            {
                Parent.AddComponentNeedingUpdate(this);

                _objectPackage = Engine.Get().ResourceManager.FetchPackage("objects/" + _objectType);
                _state = State.WaitingForPackage;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public override void OnCleanup()
        {
            Parent.RemoveComponentNeedingUpdate(this);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="time"></param>
        public override void Update(EngineTime time)
        {
            switch (_state)
            {
                case State.WaitingForPackage:
                    UpdateWaitingForPackage();
                    break;

                case State.DrawObject:
                    UpdateDrawObject();
                    break;


            }
        }


        void UpdateWaitingForPackage()
        {
            if (_objectPackage.IsComplete)
            {
                _state = State.DrawObject;
            }
        }

        void UpdateDrawObject()
        {
            GraphicAppearenceComponent graphicAppearance = null;

            if (Parent.GetEC(ref graphicAppearance))
            {
                string tilePath = _rotation.ToString() +  ".png";
                WriteableBitmap objectItem = _objectPackage.GetBitmap(tilePath);
                Vector2 objectLocation = IsometricHelper.WorldSpaceToScreenSpace(_position, objectItem.PixelWidth, objectItem.PixelHeight);
                int zindex = IsometricHelper.CalculateTileDepth(_position) + IsometricSettings.FloorObjectZOffset;
                graphicAppearance.QueueWrite(zindex, objectItem, objectLocation);
            }
            else
            {
                _state = State.Idle;
            }
           
        }

    }
}
